import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import { isEnableSso, callback } from '@/utils/sso'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/callback',
    name: 'callback',
    component: {
      created() {
        callback()
      },
      render: h => h('h2', {
        style: {
          lineHeight: '100vh',
          textAlign: 'center'
        }
      }, '登录中……')
    }
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '我的工作台', icon: 'dashboard' }
    }]
  }
]

export const asyncRoutes = [
  // 组织架构管理
  {
    path: '/orgStructure',
    redirect: '/orgStructure/org-tree-list',
    component: Layout,
    meta: { title: '组织管理', icon: 'el-icon-s-grid', permission: ['orgStructure'] },
    children: [
      {
        path: '/orgStructure/org',
        name: 'OrgIndex',
        component: () => import('@/views/orgStructure/org/Index'),
        meta: { title: '组织机构管理', permission: ['IamOrg'] }
      },
      {
        path: '/orgStructure/position',
        name: 'PositionIndex',
        component: () => import('@/views/orgStructure/position/list'),
        meta: { title: '岗位管理', permission: ['IamPosition'] }
      },
      {
        path: '/orgStructure/orgUser',
        name: 'OrgUserIndex',
        component: () => import('@/views/orgStructure/orgUser/Index'),
        meta: { title: '组织人员管理', permission: ['IamOrgUser'] }
      }
    ]
  },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/dictionary/list',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-setting', permission: ['system'] },
    children: [
      {
        path: 'dictionary/list',
        name: 'DictList',
        component: () => import('@/views/system/dictionary/list'),
        meta: { title: '数据字典管理', permission: ['Dictionary'] }
      },
      {
        path: 'iamUser/index',
        name: 'IamUserIndex',
        component: () => import('@/views/system/iamUser/Index'),
        meta: { title: '系统用户管理', permission: ['IamUser'] }
      },
      {
        path: 'iamRole/list',
        name: 'IamRoleList',
        component: () => import('@/views/system/iamRole/list'),
        meta: { title: '角色权限管理', permission: ['IamRole'] }
      },
      {
        path: 'iamResourcePermission/list',
        name: 'IamResourcePermission',
        component: () => import('@/views/system/iamResourcePermission/list'),
        meta: { title: '资源权限管理', permission: ['IamResourcePermission'] }
      },
      {
        path: 'messageTemplate/list',
        name: 'messageTemplateList',
        component: () => import('@/views/system/messageTemplate/list'),
        meta: { title: '消息模版管理', keepAlive: true, permission: ['MessageTemplate'] }
      },
      {
        path: 'message/list',
        name: 'messageList',
        component: () => import('@/views/system/message/list'),
        meta: { title: '消息记录管理', keepAlive: true, permission: ['Message'] }
      },
      {
        path: 'scheduleJob/list',
        name: 'ScheduleJobIndex',
        component: () => import('@/views/system/scheduleJob/list'),
        meta: { title: '定时任务管理', permission: ['ScheduleJob'] }
      },
      {
        path: 'uploadFile/list',
        name: 'uploadFileList',
        component: () => import('@/views/system/uploadFile/list'),
        meta: { title: '上传文件管理', keepAlive: true, permission: ['UploadFile'] }
      },
      {
        path: 'config',
        name: 'systemConfig',
        component: () => import('@/views/system/config/index'),
        meta: { title: '系统配置管理', keepAlive: true, permission: ['SystemConfig'] }
      },
      {
        path: 'iamOperationLog/list',
        name: 'IamOperationLogList',
        component: () => import('@/views/system/iamOperationLog/list'),
        meta: { title: '操作日志查看', permission: ['IamOperationLog'] }
      },
      {
        path: 'iamLoginTrace/list',
        name: 'IamLoginTraceList',
        component: () => import('@/views/system/iamLoginTrace/list'),
        meta: { title: '登录日志查看', permission: ['IamLoginTrace'] }
      }
    ]
  },

  {
    path: '/account',
    component: Layout,
    redirect: '/account/setting',
    name: 'Account',
    meta: { title: '个人中心', icon: 'people' },
    children: [
      {
        path: 'setting',
        name: 'AccountSetting',
        component: () => import('@/views/account/Setting'),
        meta: { title: '基本设置' }
      },
      {
        path: 'changePwd',
        name: 'AccountChangePwd',
        component: () => import('@/views/account/ChangePwd'),
        meta: { title: '更改密码' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: isEnableSso() ? 'history' : 'hash', // 使用SSO时只能是history模式
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router


const generateRouterMap = [
  {
    path: '/basic',
    redirect: '/basic',
    component: Layout,
    alwaysShow: true,
    name: 'basic',
    meta: { title: '基础管理', icon: 'el-icon-s-home', permission: [ 'basic' ] },
    children: [
      {
        path: 'express/list',
        name: 'express',
        component: () => import('@/views/express/list'),
        meta: { title: '物流管理', keepAlive: true, permission: [ 'express' ] }
      },
      {
        path: 'classifyGoods/list',
        name: 'classifyGoods',
        component: () => import('@/views/classifyGoods/list'),
        meta: { title: '产品分类管理', keepAlive: true, permission: [ 'classifyGoods' ] }
      },
      {
        path: 'goods/list',
        name: 'goods',
        component: () => import('@/views/goods/list'),
        meta: { title: '产品管理', keepAlive: true, permission: [ 'goods' ] }
      },
      {
        path: 'property/list',
        name: 'property',
        component: () => import('@/views/property/list'),
        meta: { title: '资产管理', keepAlive: true, permission: [ 'property' ] }
      },
      {
        path: 'unit/list',
        name: 'unit',
        component: () => import('@/views/unit/list'),
        meta: { title: '单位管理', keepAlive: true, permission: [ 'unit' ] }
      },
      {
        path: 'storeroom/list',
        name: 'storeroom',
        component: () => import('@/views/storeroom/list'),
        meta: { title: '仓库管理', keepAlive: true, permission: [ 'storeroom' ] }
      },
      {
        path: 'shelves/list',
        name: 'shelves',
        component: () => import('@/views/shelves/list'),
        meta: { title: '仓库货架管理', keepAlive: true, permission: [ 'shelves' ] }
      },
      {
        path: 'partner/list',
        name: 'partner',
        component: () => import('@/views/partner/list'),
        meta: { title: '第三方合作者', keepAlive: true, permission: [ 'partner' ] }
      },
      {
        path: 'supplier/list',
        name: 'supplier',
        component: () => import('@/views/supplier/list'),
        meta: { title: '供应商', keepAlive: true, permission: [ 'supplier' ] }
      }
    ]
  },
  {
    path: '/order',
    redirect: '/order',
    component: Layout,
    alwaysShow: true,
    name: 'order',
    meta: { title: '订单管理', icon: 'el-icon-s-promotion', permission: [ 'Order' ] },
    children: [
      // {
      //   path: 'stageName/list',
      //   name: 'stageNameList',
      //   component: () => import('@/views/billing/stageName/list'),
      //   meta: { title: '阶段配置管理', keepAlive: true, permission: ['StageName'] }
      // },
      // {
      //   path: 'review/list',
      //   name: 'reviewList',
      //   component: () => import('@/views/billing/review/list'),
      //   meta: { title: '审核管理', keepAlive: true, permission: ['Review'] }
      // },
      // {
      //   path: 'stage/list',
      //   name: 'stageList',
      //   component: () => import('@/views/billing/stage/list'),
      //   meta: { title: '阶段管理', keepAlive: true, permission: ['Stage'] }
      // },
      {
        path: 'customer/list',
        name: 'customerList',
        component: () => import('@/views/customer/list'),
        meta: { title: '客户管理', keepAlive: true, permission: [ 'Customer' ] }
      },
      {
        path: 'orders/list',
        name: 'ordersList',
        component: () => import('@/views/order/list'),
        meta: { title: '订单管理', keepAlive: true, permission: [ 'Orders' ] }
      },
      {
        path: 'ordersku/list',
        name: 'orderskuList',
        component: () => import('@/views/ordersku/list'),
        meta: { title: '订单项管理', keepAlive: true, permission: [ 'OrderSku' ] }
      },
      // {
      //   path: "/billing/bidding/details",
      //   component: () => import("@/views/billing/bidding/detail"),
      //   name: "BillingBiddingDetails",
      //   hidden: true,
      //   meta: {
      //     title: "招投标项目详情",
      //     roles: [ 'Bidding' ],
      //   },
      // },

    ]
  },
  {
    path: '/procurement',
    redirect: '/procurementPlan/list',
    component: Layout,
    alwaysShow: true,
    name: 'procurement',
    meta: { title: '采购管理', icon: 'el-icon-office-building', permission: [ 'procurement' ] },
    children: [

      {
        path: 'procurementPlan/list',
        name: 'procurementPlanList',
        component: () => import('@/views/procurementPlan/list'),
        meta: { title: '采购计划', keepAlive: true, permission: [ 'procurementPlan' ] }
      },
      {
        path: 'procurementPlanItem/list',
        name: 'procurementPlanItemList',
        component: () => import('@/views/procurementPlanItem/list'),
        meta: { title: '采购计划项', keepAlive: true, permission: [ 'procurementPlanItem' ] }
      },
      {
        path: 'procurementOrder/list',
        name: 'procurementOrderList',
        component: () => import('@/views/procurementOrder/list'),
        meta: { title: '采购单', keepAlive: true, permission: [ 'procurementOrder' ] }
      },
      {
        path: 'procurementOrderItem/list',
        name: 'procurementOrderItemList',
        component: () => import('@/views/procurementOrderItem/list'),
        meta: { title: '采购单项', keepAlive: true, permission: [ 'procurementOrderItem' ] }
      }
      

      
    ]
  },
 

  {
    path: '/stock',
    redirect: '/procurementStock/list',
    component: Layout,
    alwaysShow: true,
    name: 'titck',
    meta: { title: '库存管理', icon: 'el-icon-user-solid', permission: [ 'Stock' ] },
    children: [

      {
        path: 'procurementStock/list',
        name: 'procurementStockList',
        component: () => import('@/views/procurementStock/list'),
        meta: { title: '采购单库存', keepAlive: true, permission: [ 'ProcurementStock' ] }
      },
      {
        path: 'orderStock/list',
        name: 'orderStockList',
        component: () => import('@/views/orderStock/list'),
        meta: { title: '订单库存', keepAlive: true, permission: [ 'OrderStock' ] }
      },
      {
        path: 'orderOut/list',
        name: 'orderOutList',
        component: () => import('@/views/orderOut/list'),
        meta: { title: '订单出库记录', keepAlive: true, permission: [ 'OrderOut' ] }
      },
      {
        path: 'productionOrder/list',
        name: 'productionOrderList',
        component: () => import('@/views/productionOrder/list'),
        meta: { title: '生产任务', keepAlive: true, permission: [ 'ProductionOrder' ] }
      },
      {
        path: 'productionOrderMaterial/list',
        name: 'productionOrderMaterialList',
        component: () => import('@/views/productionOrderMaterial/list'),
        meta: { title: '生产领料', keepAlive: true, permission: [ 'ProductionOrderMaterial' ] }
      },
      {
        path: 'productionReport/list',
        name: 'productionReportList',
        component: () => import('@/views/productionReport/list'),
        meta: { title: '生产报工', keepAlive: true, permission: [ 'ProductionReport' ] }
      },
      {
        path: 'productionMaterialUsage/list',
        name: 'productionMaterialUsageList',
        component: () => import('@/views/productionMaterialUsage/list'),
        meta: { title: '用料登记', keepAlive: true, permission: [ 'ProductionMaterialUsage' ] }
      },
      {
        path: 'productionMaterialReturn/list',
        name: 'productionMaterialReturnList',
        component: () => import('@/views/productionMaterialReturn/list'),
        meta: { title: '生产退料', keepAlive: true, permission: [ 'ProductionMaterialReturn' ] }
      },
      {
        path: 'outsourcedOrder/list',
        name: 'outsourcedOrderList',
        component: () => import('@/views/outsourcedOrder/list'),
        meta: { title: '委外加工', keepAlive: true, permission: [ 'OutsourcedOrder' ] }
      }
      
    ]
  },
  {
    path: '/finance',
    redirect: '/financeReceipt/list',
    component: Layout,
    alwaysShow: true,
    name: 'finance',
    meta: { title: '财务管理', icon: 'el-icon-toilet-paper', permission: [ 'Finance' ] },
    children: [
      {
        path: 'financeAccountsReceivable/list',
        name: 'financeAccountsReceivableList',
        component: () => import('@/views/financeAccountsReceivable/list'),
        meta: { title: '应收账款', keepAlive: true, permission: [ 'FinanceAccountsReceivable' ] }
      },
      {
        path: 'financeReceipt/list',
        name: 'financeReceiptList',
        component: () => import('@/views/financeReceipt/list'),
        meta: { title: '收款单', keepAlive: true, permission: [ 'FinanceReceipt' ] }
      },
      

      {
        path: 'financePayable/list',
        name: 'financePayableList',
        component: () => import('@/views/financePayable/list'),
        meta: { title: '应付账款', keepAlive: true, permission: [ 'FinancePayable' ] }
      },
      {
        path: 'financePay/list',
        name: 'financePayList',
        component: () => import('@/views/financePay/list'),
        meta: { title: '付款单', keepAlive: true, permission: [ 'FinancePay' ] }
      },
      {
        path: 'financeRevExp/list',
        name: 'financeRevExpList',
        component: () => import('@/views/financeRevExp/list'),
        meta: { title: '日常收支管理', keepAlive: true, permission: [ 'FinanceRevExp' ] }
      }

    ]
  }
 
]
asyncRoutes.splice(0, 0, ...generateRouterMap)
